﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.TranslateApprove.BLL;
using Yipusoft.TranslateApprove.DAL;

public partial class Police_Select_TransactList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            List<Guid> delSelects = new List<Guid>();
            ViewState["Selects"] = delSelects;
            ListPageContral1.SetFirstPage();
            BindGV(0);

        }


    }

    public void BindGV(int pageIndex)
    {
        UserGrid.Caption = "<span>调动人员审核</span>";
        //调入时不显示联系人人人

        if (rblTpye.SelectedValue == "1")
        {
            UserGrid.Columns[8].Visible = false;
        }
        else
        {
            UserGrid.Columns[8].Visible = true;
        }
        if (rblTpye.SelectedValue == "2")
        {
            UserGrid.Columns[9].Visible = false;
            UserGrid.Columns[10].Visible = true;
        }
        else
        {
            UserGrid.Columns[9].Visible = true;
            UserGrid.Columns[10].Visible = false;
        }
        List<TranslateSelect> list = new List<TranslateSelect>();
        int itemCount = 0;
        string where = lblQuery.Text;
        if (ddlState.SelectedValue == "2")
        {
            itemCount = Select.GetSelectCount(where, Convert.ToInt32(rblTpye.SelectedValue), 2);
            list = Select.GetSelectList(where, Convert.ToInt32(rblTpye.SelectedValue), pageIndex, ListPageContral1.PageSize, 2).ToList();
        }
        else
        {
            itemCount = Select.GetSelectCount(where, Convert.ToInt32(rblTpye.SelectedValue));
            list = Select.GetSelectList(where, Convert.ToInt32(rblTpye.SelectedValue), pageIndex, ListPageContral1.PageSize).ToList();
        }
        ListPageContral1.ItemCount = itemCount;
        UserGrid.DataSource = list;
        UserGrid.DataBind();
    }

    protected void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        BindGV(PageIndex);
    }

    protected void UserGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = ((ListPageContral1.CurrentPageIndex * ListPageContral1.PageSize) + (e.Row.RowIndex + 1)).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
            TranslateSelect select = e.Row.DataItem as TranslateSelect;
            Label lblRank = e.Row.FindControl("lblRank") as Label;
            lblRank.Text = select.Rank.Trim() == "" ? "" : Yipusoft.Common.BLL.CodeItem.GetCodeItem(select.Rank).ItemName;
            CheckBox CheckItem = e.Row.FindControl("CheckItem") as CheckBox;
            List<Guid> Selects = ViewState["Selects"] as List<Guid>;
            if (Selects.Contains(select.Id))
            {
                CheckItem.Checked = true;
            }
            else
            {
                CheckItem.Checked = false;
            }
            //当前状态
            Label lblState = e.Row.FindControl("lblState") as Label;
            SetState(select.SelectLogs.Max(o => o.State).Value, lblState);
        }
    }

    protected void UserGrid_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }

    protected void lkbCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("ApproveList.aspx?gid=" + Request["id"]);
    }


    void SetState(int State, Label lbl)
    {
        switch (State)
        {
            case 0:

                lbl.Text = "警务处长审核中..."; break;
            case 1:

                lbl.Text = "参谋长审核中..."; break;
            case 2:
                lbl.Text = "审核通过";
                lbl.ForeColor = System.Drawing.Color.Red;
                break;
            case 3:
                lbl.Text = "已办理";
                lbl.ForeColor = System.Drawing.Color.Green;
                break;
            default:
                break;
        }
    }


    //办理
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        SelectDataContext db = new SelectDataContext();
        String  SelectID = "";
        String SoldierId = "";
        foreach (GridViewRow row in UserGrid.Rows)
        {
            Guid id = new Guid(UserGrid.DataKeys[row.DataItemIndex].Value.ToString());
            TranslateSelect info = db.TranslateSelect.Where(o => o.Id == id).FirstOrDefault();
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox CheckItem = row.FindControl("CheckItem") as CheckBox;
                if (CheckItem.Checked && info.SelectLogs.Max(o => o.State) == 2)//
                {
                    
                        
                        //Response.Redirect("../TranslateProcess/TranslateInfo.aspx?SelectId=" + UserGrid.DataKeys[row.DataItemIndex].Value.ToString());
                        SoldierId += "," + info.SoldierId;
                        if (rblTpye.SelectedValue == "0")
                        {
                            SelectID += "," + UserGrid.DataKeys[row.DataItemIndex].Value.ToString();
                        }
                        else
                        {
                            SelectID = UserGrid.DataKeys[row.DataItemIndex].Value.ToString();
                        }
                    
                }

            }
        }
        if (rblTpye.SelectedValue == "0")
        {
            Response.Redirect("../TranslateProcess/TranslateInfo.aspx?SelectId=" + SelectID);
        }
        if (rblTpye.SelectedValue == "1")
        {
            Response.Redirect("../TranslateProcess/TanslateOut.aspx?id=" + SoldierId + "&SelectId=" + SelectID);
        }
        if (rblTpye.SelectedValue == "2")
        {
            Response.Redirect("../TranslateProcess/InnerTranslate.aspx?id=" + SoldierId + "&SelectId=" + SelectID);
        }

    }

    protected void rblTpye_SelectedIndexChanged(object sender, EventArgs e)
    {
        ListPageContral1.SetFirstPage();
        lblQuery.Text = "";
        BindGV(0);
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        ListPageContral1.SetFirstPage();
        BindGV(0);
    }
    protected void btnQuery_Click(object sender, EventArgs e)
    {
        lblQuery.Text = txtQuery.Text;
        txtQuery.Text = "";
        BindGV(0);
    }
}
